package com.xb.cloud.merchantapi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService;
import com.xb.cloud.product.merchant.service.client.input.bo.ShopPromotionSkuInput;

/**
 * 
 * @ClassName: ShopPromotionSkuAdapter
 * @Description:TODO
 * @author: 鲜备科技 . tandongmei
 * @date: 2018年3月1日 下午2:38:10
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */

@Component
public class ShopPromotionSkuAdapter extends Adapter {

	private static final Logger logger = LoggerFactory.getLogger(ShopPromotionSkuAdapter.class);

	@Resource
	private ShopPromotionSkuApiService shopPromotionSkuApiService;

	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;
		/**
		 * 1. 商品推荐_新增一条记录
		 */
		if ("/h5/merchant/shopPromotionSku/createShopPromotionSku".equalsIgnoreCase(urlPath)) {
			ShopPromotionSkuInput shopInput = null;

			try {
				String shopInputString = bizInfo.get("shopInput");
				shopInput = JSON.parseObject(shopInputString, ShopPromotionSkuInput.class);
				if (StringUtils.isEmpty(shopInputString)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			Long endMerchantUserId = context.getCurrentMerchantUserId(); // 当前登录用户
			// 调取Api service
			rtnInfo = shopPromotionSkuApiService.createShopPromotionSku(shopInput, endMerchantUserId);

		}

		/**
		 * 2.商品推荐_批量新增
		 */
		if ("/h5/merchant/shopPromotionSku/createShopPromotionSkuBatch".equalsIgnoreCase(urlPath)) {
			List<ShopPromotionSkuInput> shopPromotionSkuInputList = new ArrayList<ShopPromotionSkuInput>();

			try {
				String shopInputString = bizInfo.get("shopInputList");
				shopPromotionSkuInputList = JSON.parseArray(shopInputString, ShopPromotionSkuInput.class);
				if (StringUtils.isEmpty(shopInputString)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId = context.getCurrentMerchantUserId(); // 当前登录用户
			rtnInfo = shopPromotionSkuApiService.createShopPromotionSkuList(shopPromotionSkuInputList,
					endMerchantUserId);

		}

		/**
		 * 3.商品推荐_批量新增和批量修改统一调用该接口
		 */
		if ("/h5/merchant/shopPromotionSku/createOrUpdateShopPromotionSkuBatch".equalsIgnoreCase(urlPath)) {
			List<ShopPromotionSkuInput> shopPromotionSkuInputList = new ArrayList<ShopPromotionSkuInput>();

			try {
				String shopInputString = bizInfo.get("shopInputList");
				shopPromotionSkuInputList = JSON.parseArray(shopInputString, ShopPromotionSkuInput.class);
				if (StringUtils.isEmpty(shopInputString)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId = context.getCurrentMerchantUserId(); // 当前登录用户
			rtnInfo = shopPromotionSkuApiService.createOrUpdateShopPromotionSkuBatch(shopPromotionSkuInputList,
					endMerchantUserId);

		}

		/**
		 * 4.商品推荐_批量新增，批量修改，批量删除统一调用该接口
		 */
		if ("/h5/merchant/shopPromotionSku/handleShopPromotionSkuBatch".equalsIgnoreCase(urlPath)) {
			List<ShopPromotionSkuInput> shopPromotionSkuInputList = new ArrayList<ShopPromotionSkuInput>();

			try {
				String shopInputString = bizInfo.get("shopInputList");
				shopPromotionSkuInputList = JSON.parseArray(shopInputString, ShopPromotionSkuInput.class);
				if (StringUtils.isEmpty(shopInputString)) { 
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId = context.getCurrentMerchantUserId(); // 当前登录用户
			rtnInfo = shopPromotionSkuApiService.handleShopPromotionSkuBatch(shopPromotionSkuInputList,
					endMerchantUserId);

		}

		/**
		 * 5.商品推荐_sku列表分页查询
		 */
		if ("/h5/merchant/shopPromotionSku/getShopPromotionSkuList".equalsIgnoreCase(urlPath)) {
			String pageNum = "";
			String pageSize = "";
			try {
				pageNum = bizInfo.get("pageNum");
				pageSize = bizInfo.get("pageSize");
				if (StringUtils.isEmpty(pageNum) && StringUtils.isEmpty(pageSize)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId = context.getCurrentMerchantUserId(); // 当前登录用户
			
			rtnInfo = shopPromotionSkuApiService.getShopPromotionSkuList(pageNum, pageSize, endMerchantUserId);

		}
		

		/**
		 * 6.用户推荐_删除一条记录
		 */
		if ("/h5/merchant/shopPromotionSku/deleteShopPromotionSku".equalsIgnoreCase(urlPath)) {
			String id = bizInfo.get("id");
			rtnInfo = shopPromotionSkuApiService.deleteShopPromotionSku(id);
		}

		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/merchant/shopPromotionSku/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}

}
